package com.zznode.redsun.smartcrm.business.service.impl;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.zznode.csp.domain.basemodel.DomainCategory;
import com.zznode.csp.domain.commons.query.JdbcFinderManager;
import com.zznode.csp.domain.commons.util.MapCreaterBuilder;
import com.zznode.redsun.smartcrm.business.service.JdbcDictService;

@Service
public class JdbcDictServiceImpl implements JdbcDictService {

	@Autowired
	private JdbcFinderManager jdbcFinderManager;

	@Override
	public <T extends DomainCategory> String dictName(Class<T> clazz, String pkid) {
		if (null == clazz || !StringUtils.hasText(pkid)) {
			return null;
		}
		String querySql = jdbcFinderManager.getQueryStringByStatementKey("JdbcDictService_dictName");
		return jdbcFinderManager.queryForObject(querySql, MapCreaterBuilder.create("category", clazz.getSimpleName())
				.put("pkid", pkid).map(), new RowMapper<String>() {

			@Override
			public String mapRow(ResultSet rs, int arg1) throws SQLException {
				return rs.getString("name");
			}
		});
	}
}
